<template>
  <view class="goods-list">
    <view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
      <my-goods :goods="goods"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '', // 关键字查询
          cid: '', // 分类查询,
          pagenum: 1, // 页码 
          pagesize: 10, // 页面大小
        },
        goodsList: [], // 产品列表
        total: 0, // 当前请求参数返回商品总数
        
        isloading: false, // 列表数据请求状态
      };
    },
    onLoad(options) {
      this.queryObj.cid = options.cid || '',
      this.queryObj.query = options.query || ''
      
      // 发起请求
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        this.isloading = true
        const {data: res} = await uni.$http.get('/goods/search', this.queryObj)
        this.isloading = false
        
        cb && cb()
        
        console.log(res)
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 赋值数据到data中
        this.total= res.message.total
        this.goodsList =  [...this.goodsList, ...res.message.goods] 
        
      },
      // 跳转到商品详情
      gotoDetail(goods){
        console.log(goods)
        uni.navigateTo({
          url: `/subpkg/goods_detail/goods_detail?goods_id=${goods.goods_id}`
        })
      }
    },
    // 触底
    onReachBottom() {
      console.log('到底了')
      // 当前页面数据是否加载完毕判断
      if(this.isloading) return
      
      // 当前查询条件全部数据是否加载完毕判断
      if(this.goodsList.length >= this.total){
        return uni.$showMsg('数据加载完毕！')
      }
      
      this.queryObj.pagenum += 1
      
      // 发起请求
      this.getGoodsList()
      
    },
    // 下拉刷新
    onPullDownRefresh() {
      // 清空条件
      this.queryObj.pagenum = 1
      this.queryObj.pagesize= 10
      this.goodsList = []
      this.total = 0
      this.isloading = false
      
      //发起请求
      this.getGoodsList(() => uni.stopPullDownRefresh())
      
    }
  }
</script>

<style lang="scss">
  .goods-list {
  
  }
</style>
